package com.xtedu.examproject.controller;

import com.xtedu.examproject.entity.StudentExamRecordDTO;
import com.xtedu.examproject.entity.User;
import com.xtedu.examproject.service.StudentExamRecordService;
import com.xtedu.examproject.util.RedisUtil;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import com.xtedu.examproject.service.MenuService;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;

import java.io.IOException;
import java.util.List;

@WebServlet("/default")
public class DefaultServlet extends HttpServlet {
    private MenuService menuService;

    public void init() {
        menuService = new MenuService();
    }

    public void doGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException {

        try {

// 从Redis获取用户信息
            String sessionId = request.getSession().getId();
            User user = RedisUtil.getObject("session:" + sessionId, User.class);
            int roleId =0;
            if (user != null) {
                roleId = user.getRoleId();
            }
            if (roleId == 4) {
                int infoId = user.getInfoId();
                List<StudentExamRecordDTO> records = StudentExamRecordService.getStudentExamRecordsByStudentId(infoId);
                HttpSession session = request.getSession();
                session.setAttribute("stuId", infoId);
                request.setAttribute("records", records);
                request.getRequestDispatcher("/WEB-INF/views/stuExamRecords.jsp").forward(request, response);
            } else {
                request.getRequestDispatcher("/WEB-INF/views/default.jsp").forward(request, response);
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
